package com.lp.controller.iot;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lp.bo.IotHistoryTriggerInfoBO;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;


@Controller
public class IotHistoryTriggerInfoController extends BaseController {

	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO_PAGE)
	public ModelAndView selectPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotHistoryTriggerInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			// 这边查询，需要有场景id
			if( ObjectUtil.isNotEmpty(obj.getScene_id()) ){
				resultMap = service.selectPageList("IotHistoryTriggerInfo.selectPage",getPageBean(paged,pageSize), obj);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * Excel 下载
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO_EXCEL )
	public void excel(HttpServletResponse response, 
			IotHistoryTriggerInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			downExcel("IotHistoryTriggerInfo.select", "触发历史下载", "tpl/xls/history_action_template", obj, response);
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
	}
	
	/**
	 * 插入
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO)
	public ModelAndView save(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotHistoryTriggerInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			
			resultMap = service.insert("IotHistoryTriggerInfo.insert", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 查询单个
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO)
	public ModelAndView selectOne(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			
			resultMap = service.selectOne("IotHistoryTriggerInfo.selectOne", new IotHistoryTriggerInfoBO(id)) ;
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 更新
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO)
	public ModelAndView update(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotHistoryTriggerInfoBO obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = service.update("IotHistoryTriggerInfo.update", obj) ;
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 删除
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.TriggerHistoryInfo.TRIGGER_HISTORY_INFO)
	public ModelAndView delete(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			IotHistoryTriggerInfoBO obj = new IotHistoryTriggerInfoBO();
			if(ObjectUtil.isEmpty(id)){
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.PARAM_ERROR);
			}else{
				obj.setId(id);
				obj.setDelete_flag(Constants.DELETE.YES);
				resultMap = service.update("IotHistoryTriggerInfo.update", obj) ;		
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}
}
